package com.yeyks.commonReference.append.statistic.impl;

import cn.hutool.core.util.StrUtil;
import com.yeyks.common.constants.activity.RedisKeyPreConstant;
import com.yeyks.commonReference.append.statistic.StatisticBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

/*
统计基础类
 */
@Service
public class StatisticBaseServiceImpl implements StatisticBaseService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 添加统计数据自增set
     *
     * @param middle 统计项目
     * @param end    统计子项
     * @param key    唯一标识
     * @return
     */
    @Override
    public boolean statisticIncrement(String middle, String end, String key) {
        return redisTemplate.opsForHash().increment(RedisKeyPreConstant.STATISTICAL_DATA_PRE + middle + end, key, 1) > 0;
    }

    /**
     * 统计抽奖次数
     *
     * @param middle 统计项目
     * @param end    统计子项
     * @param key    唯一标识
     * @return
     */
    @Override
    public Integer statisticCount(String middle, String end, String key) {
        try {
            Map<String, String> resultMap = redisTemplate.opsForHash().entries(RedisKeyPreConstant.STATISTICAL_DATA_PRE + middle + end);
            String count = resultMap.get(key);
            if (StrUtil.isBlank(count)) {
                return 0;
            }
            return Integer.valueOf(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

}
